package com.craftworks.tiles.impl;

import cn.hutool.core.date.ChineseDate;
import com.craftworks.tiles.AbstractTile;

import java.awt.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class ClockTile extends AbstractTile
{
    private LocalDateTime time;
    private FontMetrics fmTime;

    private static final Font FONT_TIME = new Font("Inter Variable Text", Font.PLAIN, 32);
    private static final Font FONT_DATE = new Font("Source Han Sans CN", Font.PLAIN, 16);
    private static final Font FONT_WEEKDAY = new Font("Source Han Sans CN", Font.PLAIN, 14);
    private static final Color BACKGROUND = Color.decode("#18181A");
    private static final DateTimeFormatter FORMATTER_0 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter FORMATTER_1 = DateTimeFormatter.ofPattern("HH:mm:ss");
    private static final DateTimeFormatter FORMATTER_2 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    private static final DateTimeFormatter FORMATTER_3 = DateTimeFormatter.ofPattern("eeee");

    public ClockTile()
    {
        this(LocalDateTime.now());
    }

    public ClockTile(String time)
    {
        this(LocalDateTime.parse(time, FORMATTER_0));
    }

    public ClockTile(LocalDateTime time)
    {
        super(Size.WIDE_LANDSCAPE);

        setProperty("time", time);
    }

    @Override
    public List<String> propertyKeys()
    {
        return List.of("time");
    }

    @Override
    public void setProperty(String key, Object value)
    {
        if ("time".equals(key))
        {
            switch (value)
            {
                case null -> time = LocalDateTime.now();
                case LocalDateTime t -> time = t;
                case String s -> time = LocalDateTime.parse(s, FORMATTER_0);
                default -> throw new IllegalStateException("Unexpected value: " + value);
            }
            repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        g2d.setPaint(BACKGROUND);
        g2d.fillRect(0, 8, size.width, 42);

        g2d.setPaint(FOREGROUND);

        if (time == null)
            time = LocalDateTime.now();

        if (fmTime == null)
            fmTime = g2d.getFontMetrics(FONT_TIME);
        String timeStr = time.toLocalTime().format(FORMATTER_1);
        int width = fmTime.stringWidth(timeStr);
        g2d.setFont(FONT_TIME);
        g2d.drawString(timeStr, (size.width - width) / 2, 40);

        g2d.setFont(FONT_DATE);
        g2d.drawString(time.toLocalDate().format(FORMATTER_2), 10, 71);

        ChineseDate chineseDate = new ChineseDate(time.toLocalDate());

        g2d.setFont(FONT_WEEKDAY);
        g2d.drawString(time.toLocalDate().format(FORMATTER_3), 100, 70);
        g2d.drawString(chineseDate.getChineseMonth() + chineseDate.getChineseDay(), 10, 92);
    }
}
